<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class pro_ktr_sewa_tempat extends MY_Controller {
	
	function __construct()
	{
		parent::__construct(); 
		
		$this->load->model('pro_ktr_sewa_tempat_model', 'Sewa');
		$this->secure_session();
	}
	
	function index(){
		$data['title'] = "Data Kontrak Sewa Tempat";
		viewByTemplate('pro_ktr_sewa_tempat/index', $data, 'grid');
	}
	
	function getdata()
	{
		$this->secure_session();
		echo $this->Sewa->GetJSON();
	}

	private function validation()
	{
		$error = new ErrorList();

		// Get Error Template
		$tpl_empty = $this->MessageTemplate("ER001");
		$tpl_duplicate = $this->MessageTemplate("ER002");
		// Validation
		
		if (post("kode_sewa") == '') {
			$error->Add("kode_sewa", "Kode Sewa", $tpl_empty);
		} else if (post("mode") == "create" && intval($this->get_data("SELECT 1 FROM pro_kontrak_sewa_tempat WHERE kode_sewa = '".@ereg_replace("[^a-zA-Z0-9_-]", "",@strtoupper(@trim(post("KODE_PEMILIK"))))."'")) > 0) {
			$error->Add("kode_sewa", "Kode Sewa", $tpl_duplicate);
		}else if (post("tanggal_sewa") == '') {
			$error->Add("tanggal_sewa", "Tanggal Sewa", $tpl_empty);
		}else if (post("kode_mitra") == '') {
			$error->Add("kode_mitra", "Kode Mitra", $tpl_empty);
		}else if (post("kode_tempat") == '') {
			$error->Add("kode_tempat", "Alamat Lengkap", $tpl_empty);
		}else if (post("masa_kontrak") == '') {
			$error->Add("masa_kontrak", "Masa Kontrak", $tpl_empty);
		}else if (post("biaya_kontrak") == '') {
			$error->Add("biaya_kontrak", "Biaya Kontrak", $tpl_empty);
		}else if (post("keterangan") == '') {
			$error->Add("keterangan", "Keterangan", $tpl_empty);
		}
		return $error;
	}
	
	function ajax_post()
	{
		$is_error = 0;
		$ls_response = "";
		$ls_genid = "";

		$error = $this->validation();
		try
		{
			if (count( $error->Get() ) > 0) { $is_error = 1; }
			else
			{
				// Fill Post Data
				$data	= post_by_key(array('kode_sewa', 'tanggal_sewa', 'kode_mitra', 'kode_tempat', 'masa_kontrak', 'biaya_kontrak', 'keterangan' ));
				$data['kode_sewa']	= @ereg_replace("[^a-zA-Z0-9_-]", "",@strtoupper(@trim($data['kode_sewa'])));
				
				if (post("mode") == "create")
				{
					$this->Sewa->Add($data);

					$ls_response = "Data Tersimpan";

				}
				else if (post("mode") == "edit")
				{
					
					$this->Sewa->Update($data, array('kode_sewa'	=> $data['kode_sewa']));

					$ls_response = "Data Tersimpan [edited]";
				}
			}
		} catch(Exception $e) {
			$is_error = 1; $ls_response = $e->getMessage();
		}
		
		echo $this->PostResponse(json_encode(array(	'is_error'	=> $is_error,
													'fields'	=> $error->GetDictionary(),
													'msg'		=> $ls_response,
													'genid'		=> $ls_genid,
													'form_sender'=> post("form_sender"))
								));
	}

	function ajax_delete()
	{
		$ls_result	= "";
		$kode_sewa	= get('kode_sewa');
		
		if ($kode_sewa != "")
		{
			// Delete Single
			$arr_rule	= array('kode_sewa' => $kode_sewa);
			$ls_result = $this->Sewa->DeleteSingle($arr_rule);

		}
		else
		{
			
			$num_row = intval($this->data['num_row']['deleted']);
			
			for ($i = 0; $i < $num_row; $i++)
			{
				$row	= $this->data['deleted'][$i];
				
				$arr_rule	= array('kode_sewa' => $row['kode_sewa']);
				$ls_result .= $this->Sewa->DeleteSingle($arr_rule);
			}
		}
		
		echo $ls_result;
	}
}
?>